package com.thread.interruptThread;

/**
 * 中断线程
 */
public class InterruptThread implements Runnable {

	private float d;

	// 线程被调用 interrupt 方法后，并没有被中断，任然在运行，所以说，interrupt 方法并不能是线程终止运行
	/*public void run() {
		while (true) {
			for (int i = 0; i < 10000000; i++) {
				d = (float) (d + (Math.PI + Math.E) / d);
			}
			System.out.println("I'm counting......");
			//转让调度器使用权
			Thread.yield();
		}
	}*/

	// 方法 1：信号量法
	public static boolean isLive = true;
	public void run() {
		while (isLive) {
			for (int i = 0; i < 10000000; i++) {
				d = (float) (d + (Math.PI + Math.E) / d);
			}
			System.out.println("I'm counting......");
			//转让调度器使用权
			Thread.yield();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		InterruptThread target = new InterruptThread();
		Thread t = new Thread(target);
		t.start();

		Thread.sleep(100);

		System.out.println("开始中断线程。。。。。。");

		// 改编信号量
		InterruptThread.isLive = false;

		boolean interrupted1 = Thread.interrupted();
		boolean interrupted = t.isInterrupted();
	}
}
